package controller

import (
	"advertising/config/mysql"
	"advertising/internal/app/grpc/protoc/advertisingc"
	"advertising/internal/app/grpc/service/advertising"
	"advertising/internal/app/web/service"
	"context"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type PublishSchedule struct{}

// Save 投放计划新增保存
func (PublishSchedule) Save(c *gin.Context) {
	var params struct {
		ID           string `json:"id"`
		Position     int8   `json:"position"`
		Name         string `json:"name"`
		StartTime    int64  `json:"start_time"`
		EndTime      int64  `json:"end_time"`
		Details      string `json:"details"`
		Gender       string `json:"gender"`
		Age          string `json:"age"`
		Tag          string `json:"tag"`
		PublishType  int8   `json:"publish_type"`
		DetailsShow  int8   `json:"details_show"`
		PublishCount int64  `json:"publish_count"`
		Materials    []struct {
			MaterialID      string `json:"material_id"`
			Duration        int64  `json:"duration"`
			PlayType        int8   `json:"play_type"`
			RegularInterval int64  `json:"regular_interval"`
		} `json:"materials"`
		DistrictID        string `json:"district_id"`
		LocationID        string `json:"location_id"`
		Devices           string `json:"devices"`
		SupportActivityID string `json:"support_activity_id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	merchantName := c.GetString("merchant_name")
	userID := c.GetString("user_id")
	username := c.GetString("username")
	_, timeountRemind, err := service.PublishSchedule{}.Save(
		c.Request.Context(),
		db,
		merchantID,
		merchantName,
		userID,
		username,
		params.ID,
		params.Position,
		params.Name,
		params.StartTime,
		params.EndTime,
		params.Details,
		params.Materials,
		params.Gender,
		params.Age,
		params.Tag,
		params.Devices,
		params.PublishType,
		params.DetailsShow,
		params.PublishCount,
		params.SupportActivityID,
	)
	response.JSON(c, gin.H{
		"timeout_remind": timeountRemind,
	}, err)
}

// List 投放计划列表
func (PublishSchedule) List(c *gin.Context) {
	var params struct {
		Name         string  `json:"name"`
		Position     int     `json:"position"`
		Status       int     `json:"status"`
		Page         int     `json:"page"`
		Limit        int     `json:"limit"`
		StartTime    int64   `json:"start_time"`
		EndTime      int64   `json:"end_time"`
		LocationID   string  `json:"location_id"`
		DistrictID   string  `json:"district_id"`
		Devices      string  `json:"devices"`
		Platform     int     `json:"platform"`      //0全部 1管理端 2小程序
		AmountThresh float32 `json:"amount_thresh"` //金额临界值
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	merchantPath := c.GetString("merchant_path")
	data, total, count, err := service.PublishSchedule{}.List(c.Request.Context(), merchantID, merchantPath, params.Name,
		params.Position, params.Status, params.StartTime, params.EndTime, params.LocationID, params.DistrictID,
		params.Devices, params.Page, params.Limit, params.Platform, params.AmountThresh)
	response.JSON(c, gin.H{
		"count": count,
		"data":  data,
		"total": total,
	}, err)
}

// Detail 投放计划详情
func (PublishSchedule) Detail(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	data, err := service.PublishSchedule{}.Detail(c.Request.Context(), merchantID, params.ID)
	response.JSON(c, data, err)
}

func (PublishSchedule) SimpleDetail(c *gin.Context) {
	data, err := service.PublishSchedule{}.SimpleDetail(c.Request.Context(), c.Query("schedule_id"))
	response.JSON(c, data, err)
}

// Submit 提交审核
func (PublishSchedule) Submit(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	err := service.PublishSchedule{}.Submit(c.Request.Context(), db, merchantID, params.ID)
	response.JSON(c, nil, err)
}

// End 终止计划
func (PublishSchedule) End(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)

	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	err := service.PublishSchedule{}.End(c.Request.Context(), db, merchantID, params.ID)
	response.JSON(c, nil, err)
}

// AuditingPublishScheduleCount 审核中计划统计
func (PublishSchedule) AuditingPublishScheduleCount(c *gin.Context) {
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	count, err := service.PublishSchedule{}.AuditingPublishScheduleCount(c.Request.Context(), db, merchantID)
	response.JSON(c, gin.H{
		"count": count,
	}, err)
}

// PublishingPublishScheduleCount 投放中计划统计
func (PublishSchedule) PublishingPublishScheduleCount(c *gin.Context) {
	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	count, err := service.PublishSchedule{}.PublishingPublishScheduleCount(c.Request.Context(), db, merchantID)
	response.JSON(c, gin.H{
		"count": count,
	}, err)
}

// ScheduleCalendar 投放计划日历
func (PublishSchedule) ScheduleCalendar(c *gin.Context) {
	var params struct {
		StartTime int64 `json:"start_time"`
		EndTime   int64 `json:"end_time"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)

	db := mysql.NewDB()
	merchantID := c.GetString("merchant_id")
	data, count, err := service.PublishSchedule{}.ScheduleCalendar(c.Request.Context(), db, merchantID, params.StartTime, params.EndTime)
	response.JSON(c, gin.H{
		"count": count,
		"data":  data,
	}, err)
}

func (PublishSchedule) Test(c *gin.Context) {

	infoReq := &advertisingc.GetPublishScheduleReq{}
	infoReq.Devices = "DDRetail327_1" // 在线设备
	infoReq.Force = true
	_, err := advertising.Server{}.GetPublishSchedule(context.Background(), infoReq)
	if err == nil {
		// len(rsp.List)
	}

}

// List 投放计划列表
func (PublishSchedule) ListOrderInfo(c *gin.Context) {
	var params struct {
		Name              string  `json:"name"`
		Position          int     `json:"position"`
		Status            int     `json:"status"`
		Page              int     `json:"page"`
		Limit             int     `json:"limit"`
		StartTime         int64   `json:"start_time"`
		EndTime           int64   `json:"end_time"`
		LocationID        string  `json:"location_id"`
		DistrictID        string  `json:"district_id"`
		Devices           string  `json:"devices"`
		Platform          int     `json:"platform"` //0全部 1管理端 2小程序
		AmountThresh      float32 `json:"amount_thresh"`
		PayStatus         int     `json:"pay_status"`
		CustomName        string  `json:"custom_name"`
		PhoneNumber       string  `json:"phone_number"`
		OutTradeNo        string  `json:"out_trade_no"`
		SupportActivityID string  `json:"support_activity_id"`
		CreatedStartTime  int64   `json:"created_start_time"`
		CreatedEndTime    int64   `json:"created_end_time"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	merchantPath := c.GetString("merchant_path")
	data, total, count, err := service.PublishSchedule{}.ListOrderInfo(c.Request.Context(), merchantID, merchantPath, params.Name,
		params.Position, params.Status, params.StartTime, params.EndTime, params.LocationID, params.DistrictID,
		params.Devices, params.Page, params.Limit, params.Platform, params.AmountThresh, params.PayStatus,
		params.CustomName, params.PhoneNumber, params.OutTradeNo, params.SupportActivityID,
		params.CreatedStartTime, params.CreatedEndTime)
	response.JSON(c, gin.H{
		"count": count,
		"data":  data,
		"total": total,
	}, err)
}

// Detail 投放计划详情
func (PublishSchedule) DetailOrderInfo(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	merchantID := c.GetString("merchant_id")
	data, err := service.PublishSchedule{}.DetailOrderInfo(c.Request.Context(), merchantID, params.ID)
	response.JSON(c, data, err)
}
